package com.test.mooan.labama.activity_fragment_boom.ln_boom;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.bean_boom.user_info_boom.LnApplyDetailInfo_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.view_boom.widget_boom.TitleView_boom;

import java.util.List;
import java.util.Map;


public class LnApplyActivity_boom extends BaseLnActivity_boom {

    private TextView loanAmountTv;
    private TextView loanTermTv;
    private TextView currencyTv;
    private LinearLayout detailsViewGroup;
    private TextView countdownMinTv;
    private TextView countdownSecTv;

    private Product product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_apply_boom);
        pageTag = "APPLY";

        try {
            product = (Product) getIntent().getSerializableExtra("product");
            if (product==null) product = Product.gtInstance(null);
        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }

        init();
    }

    private void init(){
        TitleView_boom titleView = findViewById(R.id.activity_loan_apply_view_title);
        setBackBtn_boom(titleView.getBackIb());

        titleView.setTitle(product.gtName());

        loanAmountTv = findViewById(R.id.activity_loan_apply_tv_loan_amount);
        loanTermTv = findViewById(R.id.activity_loan_apply_tv_loan_term);
        currencyTv = findViewById(R.id.activity_loan_apply_tv_currency);
        SwipeRefreshLayout refreshLayout = findViewById(R.id.activity_loan_apply_refresh_layout);

        detailsViewGroup = findViewById(R.id.activity_loan_apply_view_details_group);
        countdownMinTv = findViewById(R.id.activity_loan_apply_tv_countdown_min);
        countdownSecTv = findViewById(R.id.activity_loan_apply_tv_countdown_sec);

        findViewById(R.id.activity_loan_apply_btn_apply).setOnClickListener(v ->
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_SUBMIT_CLICK").toUpperCase(),product.gtProduct());

                HttpManager_boom.getInstance().saveUserPointForBatch();

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                HttpManager_boom.getInstance().loanApply(product.gtProduct(),product.gtToken());
            })
        );

        findViewById(R.id.activity_loan_apply_btn_apply_2).setOnClickListener(v ->
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_SUBMIT_CLICK").toUpperCase(),product.gtProduct());

                HttpManager_boom.getInstance().saveUserPointForBatch();

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                HttpManager_boom.getInstance().loanApply(product.gtProduct(),product.gtToken());
            })
        );

        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpManager_boom.getInstance().requestLoanApplyDetail(product.gtProduct(),product.gtToken());
        });

        onLoanApplyDetailResponse();

        startCountdown();
    }

    @Override
    @SuppressLint("SetTextI18n")
    public void onLoanApplyDetailResponse(){

        if (isClose()) return;

        loanAmountTv.setText(LnApplyDetailInfo_boom.getInstance().gAmount());
        currencyTv.setText(LnApplyDetailInfo_boom.getInstance().gCurrency());
        loanTermTv.setText(LnApplyDetailInfo_boom.getInstance().gDays());

        List<Map<String,String>> detailsList = LnApplyDetailInfo_boom.getInstance().gDetailItemList();

        detailsViewGroup.removeAllViews();

        View detailsView = null;
        LinearLayout detailsGroup = null;

        for (Map<String,String> details : detailsList){

            String title = Stringutil_boom.getSafeString(details.get("title"));
            String showName = Stringutil_boom.getSafeString(details.get("showName"));
            String showDetail = Stringutil_boom.getSafeString(details.get("showDetail"));
            String oldDetail = Stringutil_boom.getSafeString(details.get("oldDetail"));
            String discount = Stringutil_boom.getSafeString(details.get("discount"));

            if (detailsView==null || "YES".equalsIgnoreCase(title) || "true".equalsIgnoreCase(title)){

                if (detailsView!=null) detailsViewGroup.addView(detailsView);
                detailsView = View.inflate(this,R.layout.view_ln_details,null);
                TextView titleNameTv = detailsView.findViewById(R.id.view_ln_details_tv_title_name);
                TextView titleValueTv = detailsView.findViewById(R.id.view_ln_details_tv_title_value);
                detailsGroup = detailsView.findViewById(R.id.view_ln_details_layout_group);
                titleNameTv.setText(showName);
                titleValueTv.setText(showDetail);

            }else {

                View detailItem = View.inflate(this,R.layout.item_ln_detail,null);

                TextView nameTv = detailItem.findViewById(R.id.item_ln_detail_tv_name);
                View oldValueView = detailItem.findViewById(R.id.item_ln_detail_view_old_value);
                TextView oldValueTv = detailItem.findViewById(R.id.item_ln_detail_tv_old_value);
                TextView valueTv = detailItem.findViewById(R.id.item_ln_detail_tv_value);
                View discountView = detailItem.findViewById(R.id.item_ln_detail_view_discount);
                TextView discountTv = detailItem.findViewById(R.id.item_ln_detail_tv_discount);

                nameTv.setText(showName);
                valueTv.setText(showDetail);

                if (TextUtils.isEmpty(oldDetail) || TextUtils.isEmpty(discount)){
                    oldValueView.setVisibility(View.GONE);
                    discountView.setVisibility(View.GONE);
                }else {
                    oldValueView.setVisibility(View.VISIBLE);
                    discountView.setVisibility(View.VISIBLE);
                    oldValueTv.setText(oldDetail);
                    discountTv.setText(discount);
                }

                detailsGroup.addView(detailItem);

            }

        }

        if (detailsView!=null) detailsViewGroup.addView(detailsView);
    }

    @SuppressLint("SetTextI18n")
    private void startCountdown(){

        new Thread(() -> {

            try {
                int min = 9;
                int sec = 59;

                while (min>=0){

                    if (isClose()) return;

                    final int fMin = min;
                    final int fSec = sec;

                    runOnUiThread(() -> {
                        countdownMinTv.setText("0"+fMin);
                        countdownSecTv.setText(fSec>9 ? String.valueOf(fSec):"0"+fSec);
                    });

                    sec--;

                    if (sec<0){
                        min--;
                        sec = 59;
                    }

                    Thread.sleep(999);

                }

                if (!isClose()) startCountdown();

            } catch (Exception e) {
                if (Constant_boom.Debug) e.printStackTrace();
            }

        }).start();

    }

    public void onLoanApplyResponse(){
        HttpManager_boom.getInstance().requestUserProcess();
    }

}